#!/bin/bash
# Automate

#enviroment variables
WORKINGCOPY="ultimate-gnome";
THEME="UltimateGnome";
SOURCES="Scalable";
FOLDERS="actions apps categories devices emblems mimetypes places status stock";


#find if the working copy is read-only
	DIR=`pwd`; #could have or "/home/marco" or "/home/marco/ **UG** /Script" or also "/home/marco/ **UG**" or "/media"
	FILE=$0; #could have or "Automate.sh" or "Script/Automate.sh" or " **UG**/Script/Automate.sh" or "../home/ **UG** /Script/Automate.sh"
	#test if the working copy name is int the DIR
	BEGIN="^[/[:print:]*]*ultimate-gnome";
	LENGTH=`expr match "$DIR" $BEGIN`; #if the working copy is in DIR LENGTH != 0
	if [ "$LENGTH" = "0" ]; then #working copy is in FILE
		BEGIN="^[[:print:]*/]*ultimate-gnome-read-only";
		LENGTH=`expr match "$FILE" $BEGIN`; #if the working copy is "read-only" LENGTH != 0
		if [ "$LENGTH" = "0" ]; then #working copy is in FILE
			PROJECT=$WORKINGCOPY;
		else
			PROJECT=$WORKINGCOPY"-read-only";
		fi
	else #working copy is in DIR
		BEGIN="^[/[:print:]*]*ultimate-gnome-read-only";
		LENGTH=`expr match "$DIR" $BEGIN`; #if the working copy is "read-only" LENGTH != 0
		if [ "$LENGTH" = "0" ]; then #working copy is in FILE
			PROJECT=$WORKINGCOPY;
		else
			PROJECT=$WORKINGCOPY"-read-only";
		fi
	fi

SUBSCRIPTDIR=$PROJECT"/Script/Lib";
ICONFILESDIR=$PROJECT"/IconFiles";

export SUBSCRIPTDIR;
export ICONFILESDIR;
export THEME;
export SOURCES;
export FOLDERS;
export PROJECT;

#script variables
ERROR="Error: You do not have a working copy in your 'home' directory, or it is not called 'ultimate-gnome' (or 'ultimate-gnome-read-only').";
HINT="Try to execute the script with --help to see how to use it.";

clear;
echo "UltimateGnome Automate Script";                                    #Title
echo;




case "$1" in
	"-a" | "--all") #cleaner
		path=$HOME"/$PROJECT";
		if test -d "$path"; then                                        #check if the working copy exist
			echo "**The complete process may take up to 30 minutes.**"
			echo;
			cd $path; sh $path/Script/Lib/Tar-gz-Package-Builder.sh;        #invoke real packager
			cd $path; sh $path/Script/Lib/HTML-Previewer.sh;                #invoke previewer
			cd $path; sh $path/Script/Lib/Elements-Counter.sh;              #invoke counter
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	"-b" | "--build") #cleaner
		path=$HOME"/$PROJECT";
		if test -d "$path"; then                                        #check if the working copy exist
			cd $path; sh $path/Script/Lib/Tar-gz-Package-Builder.sh; #invoke real packager
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	"-p" | "--preview") #scalable theme
		path=$HOME"/$PROJECT";
		if test -d "$path"; then                                        #check if the working copy exist
			cd $path; sh $path/Script/Lib/HTML-Previewer.sh; #invoke previewer
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	"-c" | "--clean") #cleaner
		path=$HOME"/"$PROJECT;
		if test -d "$path"; then                                        #check if the working copy exist
			cd $path; sh $path/Script/Lib/Inkscape-Definitions-Cleaner.sh; #invoke real cleaner
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	"-s" | "--sources") #extract SVG
		path=$HOME"/$PROJECT";
		if test -d "$path"; then                                        #check if the working copy exist
			cd $path; sh $path/Script/Lib/Sources-Extractor.sh; #invoke extractor
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	"-o" | "--svg-only") #scalable theme
		path=$HOME"/$PROJECT";
		if test -d "$path"; then                                        #check if the working copy exist
			cd $path; sh $path/Script/Lib/Scalable-Only-Builder.sh; #invoke builder
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	"-n" | "--count") #counter
		path=$HOME"/$PROJECT";
		if test -d "$path"; then                                        #check if the working copy exist
			cd $path; sh $path/Script/Lib/Elements-Counter.sh; #invoke counter
		else
			echo; echo $ERROR;                                      #the working copy does not exist at this path
			echo $HINT;                                             #gives help about the error
		fi
	;;
	--help)                                                           #help
		echo "Usage: sh Automate.sh [OPTION]";
		echo;
		echo "OPTION           USAGE";
		echo "-a  --all        Complete release process (-b -p -n). About 30 minutes.";
		echo "-b  --build      Exports PNG, creates a new release package. About 25 minutes.";
		echo "-p  --preview    Create an HTML page to take screenshots of the theme.";
		echo "-c  --clean      Cleans SVG unused definitions (optimization). About 2 minutes.";
		echo "-s  --sources    Exports only SVG (unique) icons to a folder.";
		echo "-o  --svg-only   Creates a scalable-only release of the theme.";
		echo "-n  --count      Counts all the theme icons (elements and unique elements).";
		echo "    --help       Does nothing. Prints out this help.";
		echo;
		echo -n "This script is intended for developers only.";
		echo -n " Go here if you want to download the icon theme 'http://code.google.com/p/ultimate-gnome/downloads/list'.";
		echo -n " If you want to know more about this script go to 'https://sites.google.com/site/ultimate-gnome/help/automate'.";
		echo " For any bug contact me at <newmmooon@gmail.com> or report the bug to 'http://code.google.com/p/ultimate-gnome/issues/'.";
	;;
	*)                                                                 #all other flags
		echo "Error: Wrong parameters.";                                 #print error message
		echo $HINT;                                                      #suggest help
	;;
esac

echo;
echo;

exit 0
